package com.shxc.smarttrade.old.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.adapter.MasterListAdapter;
import com.shxc.smarttrade.old.bean.EventDatas;
import com.shxc.smarttrade.old.bean.HomePagerDatas;
import com.shxc.smarttrade.old.constants.EventContents;
import com.shxc.smarttrade.old.myview.RecycleViewDivider;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by jcdev1 on 2017/5/9.
 */

public class MasterFragment extends Fragment {

    private View rootView;
    private MasterListAdapter masterListAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_master, null);
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        initView();
        EventBus.getDefault().register(this);
        Glide.with(this);
        return rootView;
    }

    /**
     * 初始化View
     */
    private void initView() {

        RecyclerView rvMaster = (RecyclerView) rootView.findViewById(R.id.rv_master);
        masterListAdapter = new MasterListAdapter(getActivity());
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        rvMaster.setLayoutManager(manager);
        rvMaster.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayout.HORIZONTAL, 1, getResources().getColor(R.color.dividing_line_color)));
        rvMaster.setAdapter(masterListAdapter);
//        masterListAdapter.setMyItemCLickListener(new MasterItemClickListener() {
//            @Override
//            public void ItemViewClickListener(int posintion) {
//                startActivity(new Intent(getActivity(), SupermanDetailActivity.class));
//            }
//
//            @Override
//            public void ButtonClickListener(int poaition) {
//                Toast.makeText(getContext(), "" + poaition, Toast.LENGTH_SHORT).show();
//            }
//        });

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 必须加注解，否则无效
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventDatas event) {
        if (event.getTagOne().equals(EventContents.HOME_FRAGMENT_ONE)) {
            List<HomePagerDatas.DataBean.MastersBean> mastersBeen = (List<HomePagerDatas.DataBean.MastersBean>) event.getResponse();
            //设置适配器
            masterListAdapter.setDatas(mastersBeen);

        }
    }
}
